/**
 * 账号设置
 * @author  Jello
 * @create  2018/11/05 14:48
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var token = "";
var userToken = "";

$(function() {
	userToken = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");

	//获取个人资料
	var param = {};
	param.token = userToken;
	getUserMaterial(param, function(data) {
		var user_name = data.user_name;
		var phone = data.userTel;
		$("#nickname").html(user_name);
		$("#phone").html(phone);
		$("#oldPhone").html(phone);
	});

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#newPhone").val(); //手机号
		var checkValue = checkPhone(phoneNum);
		if(checkValue) {
			//获取验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 2;
			gainCode(param, function(resultObj) {
				if(resultObj.code == 0) {
					token = resultObj.data; //获取验证码成功后返回的token
					//开启验证码定时器
					for(i = 1; i <= 60; i++) {
						if(i == 1) {
							window.setTimeout("countDown(" + i + ")");
						} else {
							window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
						}
					}
				} else {
					$("#err_phone").html(resultObj.msg);
					$(".errormsg1").show();
					return;
				}
			});
		}
	})

	//监听输入框
	$("#newPhone").on('input propertychange', function() {
		var phone = $(this).val();
		checkPhone(phone);
	})
	$("#oldPwd").on('input propertychange', function() {
		var oldPwd = $(this).val();
		checkPwd(oldPwd, "oldPwdMsg", "errormsg4");
	})
	$("#newPwd").on('input propertychange', function() {
		var newPwd = $(this).val();
		checkPwd(newPwd, "newPwdMsg", "errormsg5");
	})
	$("#comfirmPwd").on('input propertychange', function() {
		var comfirmPwd = $(this).val();
		checkPwd(comfirmPwd, "comfirmPwdMsg", "errormsg6");
	})

	//修改手机号保存
	$("#save_btn").click(function() {
		var phoneNum = $("#newPhone").val(); //手机号
		var code = $("#code").val(); //验证码
		var flag = (checkPhone(phoneNum) && checkCode(code));
		if(flag) {
			//校验验证码接口上送
			var param = {};
			param.phone = phoneNum;
			param.type = 2;
			param.code = code;
			param.token = token;
			mateCode(param, function(mateObj) {
				if(mateObj.code == 0) {
					$.ajax({
						type: "post",
						url: serverUrl + "/userMaterial/update_usertel?userToken=" + userToken + "&status=1" + "&newUserTel=" + phoneNum,
						cache: false,
						async: false,
						contentType: "application/json; charset=utf-8",
						dataType: "json",
						crossDomain: true,
						timeout: asktime,
						success: function(data) {
							var resultObj = eval(data);
							stopScroll();
							if(resultObj.code == 0 && resultObj.data == 1) {
								$(".errorimg").attr("src", "img/icon/warn.png")
								$(".errortext").html("保存成功");
								$(".errorbox").show();
								setTimeout(function() {
									$(document).unbind("scroll");
									$(".errorbox").hide();
									window.location.reload();
								}, 800);
							} else if(resultObj.code == 10002) {
								window.location.href = "login.html";
							} else {
								$(".warnimg").attr("src", "img/icon/error.png")
								$(".warntext").html(data.msg);
								$(".warnbox").show();
								return;
							}
						},
						error: function(XMLHttpRequest, status, errorThrown) {
							stopScroll();
							$(".errorimg").attr("src", "img/icon/warn.png")
							$(".errortext").html("网络异常");
							$(".errorbox").show();
							return;
						}
					});
				} else {
					$("#err_code").html(mateObj.msg);
					$(".errormsg2").show();
				}
			})
		}
	})

	//关闭提示框
	$(".cover").click(function() {
		$(document).unbind("scroll");
		$(".warnbox").hide();
		$("#newPhone").val("");
		$("#code").val("");
	})
	$(".errcover").click(function() {
		$(document).unbind("scroll");
		$(".errorbox").hide();
	})

	$("#newPhone").focus(function() {
		$("#err_phone").html("");
		$(".errormsg1").hide();
	})
	$("#code").focus(function() {
		$("#err_code").html("");
		$(".errormsg2").hide();
	})

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	//修改密码保存
	$("#savepwd_btn").click(function() {
		var oldPwd = $("#oldPwd").val();
		var newPwd = $("#newPwd").val();
		var comfirmPwd = $("#comfirmPwd").val();
		var flag = (checkPwd(oldPwd, "oldPwdMsg", "errormsg4") && checkPwd(newPwd, "newPwdMsg", "errormsg5") && checkConfirmPwd(newPwd, comfirmPwd));
		if(flag) {
			$.ajax({
				type: "get",
				url: serverUrl + "/login/modify_psw?userToken=" + userToken + "&status=1" + "&newPsw=" + hex_md5(hex_md5(newPwd) + salt) + "&password=" + hex_md5(hex_md5(oldPwd) + salt),
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				crossDomain: true,
				timeout: asktime,
				success: function(data) {
					var resultObj = eval(data);
					stopScroll();
					if(resultObj.code == 0 && resultObj.data == 1) {
						$(".errorimg").attr("src", "img/icon/warn.png")
						$(".errortext").html("修改成功");
						$(".errorbox").show();
						setTimeout(function() {
							$(".errorbox").hide();
							window.location.href = "login.html";
						}, 800);
					} else if(resultObj.code == 10002) {
						window.location.href = "login.html";
					} else {
						$(".warnimg").attr("src", "img/icon/error.png")
						$(".warntext").html(data.msg);
						$(".warnbox").show();
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					stopScroll();
					$(".errorimg").attr("src", "img/icon/warn.png")
					$(".errortext").html("网络异常");
					$(".errorbox").show();
					return;
				}
			});
		}
	})

	$("#oldPwd").focus(function() {
		$("#oldPwd").html("");
		$(".errormsg4").hide();
	})
	$("#newPwd").focus(function() {
		$("#newPwd").html("");
		$(".errormsg5").hide();
	})
	$("#comfirmPwd").focus(function() {
		$("#comfirmPwd").html("");
		$(".errormsg6").hide();
	})

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword", companyName);
		window.location.href = "applyjob.html";
	})

	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//客服中心
	$("#service_btn1").click(function() {
		window.open("tencent://message/?uin=1423335266&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("tencent://message/?uin=2028048473&Site=lanpin123.com&Menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("tencent://message/?uin=3341118684&Site=lanpin123.com&Menu=yes");
	})
})

/**
 * 校验手机号
 */
function checkPhone(phone) {
	var reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	if(phone == "") {
		$("#err_phone").html("手机号码不能为空");
		$(".errormsg1").show();
		return;
	} else if(!reg.test(phone)) {
		$("#err_phone").html("手机号码格式错误");
		$(".errormsg1").show();
		return;
	} else {
		$("#err_phone").html("");
		$(".errormsg1").hide();
	}
	return true;
}

/**
 * 验证码倒计时
 * @param {Object} count 倒数
 */
function countDown(count) {
	if(count == 60) {
		$("#code_btn").val("获取验证码");
		$("#code_btn").attr('disabled', false);
	} else {
		left_time = 60 - count;
		$("#code_btn").attr('disabled', true);
		$("#code_btn").val(left_time + "秒重新发送");
	}
}

/**
 * 检验验证码
 * @param {Object} code
 */
function checkCode(code) {
	var reg = /^[0-9]*$/;
	if(code == "") {
		$("#err_code").html("验证码不能为空");
		$(".errormsg2").show();
		return;
	} else if(!reg.test(code)) {
		$("#err_code").html("验证码格式不正确");
		$(".errormsg2").show();
		return;
	}
	return true;
}

/**
 * 校验密码
 * @param {Object} pwd
 */
function checkPwd(pwd, showTxt, showId) {
	if(pwd == "") {
		$("#" + showTxt).html("密码不能为空");
		$("." + showId).show();
		return;
	} else if(pwd.indexOf(" ") >= 0) {
		$("#" + showTxt).html("密码中不能有空格字符");
		$("." + showId).show();
		return;
	} else if((/[\u4e00-\u9fa5]+/).test(pwd)) {
		$("#" + showTxt).html("密码不能包含中文");
		$("." + showId).show();
		return;
	} else if(pwd.length < 6 || pwd.length > 16) {
		$("#" + showTxt).html("密码长度为6-16位");
		$("." + showId).show();
		return;
	} else {
		$("#" + showTxt).html("");
		$("." + showId).hide();
	}
	return true;
}

function checkConfirmPwd(newPwd, comfirmPwd) {
	if(comfirmPwd == "") {
		$("#comfirmPwdMsg").html("确认密码不能为空");
		$(".errormsg6").show();
		return;
	} else if(newPwd != comfirmPwd) {
		$("#comfirmPwdMsg").html("密码不一致");
		$(".errormsg6").show();
		return;
	}
	return true;
}